package ink.sunflowerk.service.impl;


import ink.sunflowerk.dao.UserMapper;
import ink.sunflowerk.entity.User;
import ink.sunflowerk.exception.UserLoginException;
import ink.sunflowerk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

@Service
@Transactional
public class UserServiceImp  implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User loginMethod(String uname, String upwd) {
        User user=userMapper.getOneByUname(uname);
        if(user==null){
            throw new UserLoginException("用户名错误！");
        }
        //md5加密
        String upwdMd5= DigestUtils.md5DigestAsHex(upwd.getBytes());
        if(!upwdMd5.equals(user.getUpwd())){
            throw new UserLoginException("密码错误！");
        }
        return user;
    }

    @Override
    public void updateMethod(User user) {
        if(user.getUpwd()!=null){
            String upwdMd5= DigestUtils.md5DigestAsHex(user.getUpwd().getBytes());
            user.setUpwd(upwdMd5);
        }
        userMapper.updateOne(user);
    }
}
